﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class TestPictureController : BaseController<welcome>
    {
        //
        // GET: /TestPicture/

        public override BLL.BaseBll<welcome> bll
        {
            get
            {
                return new WelcomeBll();
            }
        }
        public override ActionResult Get(int page, int rows, welcome model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.welcome_id, rows, page, out count).Select(x => new { 
            x.welcome_id,x.welcome_imgurl,x.storeid
            }).ToList();
            return Json(new { total = count, rows = list });
        }

        public ActionResult GetOnlyOne(int id)
        {
            return Json(bll.Search(x => x.welcome_id == id).Select(x => new{
                x.welcome_id,
                x.welcome_imgurl,
                x.storeid
            }).ToList()[0],JsonRequestBehavior.AllowGet);
          
        }

        [HttpPost]
        public string PostExcelData()
        {
            //获取客户端上传的文件集合
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            //获取文件集合中的第一个文件(每次只上传一个文件)
            HttpPostedFile file = files[0];
            //定义文件存放的目标路径
            string targetDir = System.Web.HttpContext.Current.Server.MapPath("~/img");

            //组合成文件的完整路径
            string path = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file.FileName));
            //保存上传的文件到指定路径中
            file.SaveAs(path);
            return path;
        }

        [HttpPost]
        public ActionResult UploadImg()
        {

            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            if (files.Count == 0)
            {
                //model.FirstThumbnail = "无";
                //model.CommodityPictures = "无";
            }
            else
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("../Images/");
                if (files != null)
                {
                    List<string> fileNameList = new List<string>();
                    for (int i = 0; i < files.Count; i++)
                    {

                        HttpPostedFile file = files[i];
                        string filename = file.FileName;
                        fileNameList.Add(filename);
                        file.SaveAs(System.Web.HttpContext.Current.Server.MapPath("/images/") + filename);
                        file.SaveAs(path + file.FileName);
                        //model.CommodityPictures = file.FileName;

                    }
                    string fileNames = string.Join(",", fileNameList.ToArray());
                    //model.FirstThumbnail = fileNameList[0];
                    //model.CommodityPictures = fileNames;
                    //ICommodityBll b = new CommodityBll();

                }

            }
            return new RedirectResult("/Commodity/Index");
        }

    }
}
